﻿using System;
using System.Linq;
using System.Linq.Expressions;
using ASPItalia.ModelVirtualCasting.Common;
using ASPItalia.ModelVirtualCasting.ObjectModel;

namespace ASPItalia.ModelVirtualCasting.EntityFramework
{
    public class UserRepository : BaseRepository<User>, IUserRepository
	{
        public UserRepository(IContextContainer container)
            : base(container)
		{
		}

		public void InsertOnSubmit(User entity)
		{
			Context.Users.AddObject(entity);
		}

        public void Update(User entity)
		{
            Context.Users.Attach(entity);
			Context.ObjectStateManager.GetObjectStateEntry(entity).SetModified();
		}

        public void DeleteOnSubmit(User entity)
		{
            Context.Users.DeleteObject(entity);
		}

		public void SubmitChanges()
		{
			SaveChanges();
		}

        public IQueryable<User> Where(Expression<Func<User, bool>> predicate)
		{
			return CurrentObjectQuery.Where(predicate);
		}

        public long Count(Expression<Func<User, bool>> predicate)
		{
			return CurrentObjectQuery.Count(predicate);
		}

		public User GetByUsername(string username)
		{
			return CurrentObjectQuery.First(u => u.Username == username);
		}

        public IQueryable<User> GetAll()
        {
            return CurrentObjectQuery;
        }

		public IUserRepository Include(params string[] paths)
		{
			CurrentObjectQuery = CreateObjectQuery().Include(paths);
			return this;
		}

		#region IReadOnlyRepository<IUserRepository,User> Members


		public IUserRepository EnableLazyLoading()
		{
			SetLazyLoading(true);
			return this;
		}

		public IUserRepository DisableLazyLoading()
		{
			SetLazyLoading(false);
			return this;
		}

		#endregion
	}
}